package cn.zhy.org.controller;
import cn.zhy.basic.exception.BusinessException;
import cn.zhy.basic.util.ExcelUtil;
import cn.zhy.org.domain.ShopAuditLog;
import cn.zhy.org.dto.ShopDto;
import cn.zhy.org.service.IShopService;
import cn.zhy.org.domain.Shop;
import cn.zhy.org.query.ShopQuery;
import cn.zhy.basic.util.AjaxResult;
import cn.zhy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null) {
                shopService.update(shop);
            }else {
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }




    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false, "入驻失败");
        } catch (Exception e) {
            return new AjaxResult(false, "系统异常");
        }
    }

    //店铺审核通过
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log) {
        try {
            shopService.pass(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "审核失败");
        }

    }

    //店铺审核驳回
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "驳回失败失败");
        }

    }

    @GetMapping("/active/{id}")
    public void active(@PathVariable("id") Long id,HttpServletResponse response){
        try {
            shopService.success(id);
            response.sendRedirect("http://localhost:8081/#/login");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //导出数据
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //导入数据
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : list) {
            System.out.println(shop);
        }

    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.echar();
        return shopDtos;
    }


}
